package org.brownflat.lighthouse.ci.teamcity;

import org.brownflat.lighthouse.ci.Builder;
import org.brownflat.lighthouse.ci.ConnectionException;
import org.brownflat.lighthouse.ci.Builds;
import org.brownflat.lighthouse.ci.HTTPServer;
import org.brownflat.lighthouse.ci.Monitorable;

import java.io.Serializable;

public class TeamCityServer extends HTTPServer implements Monitorable, Serializable {

	private Builder<Builds, String> builder = new TeamCityBuilder();
    private String name = "TeamCityServer";

	public TeamCityServer(String host, int port) {
		super(host, port);
	}

    public TeamCityServer() {
        super("localhost", 8080);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getSummary() {
        return String.format("%s - %s", name, getURL());
    }

    @Override
    public Builds builds() {
        try {
			String result = execute(new TeamCityLatestBuildsRequest());
			return builder.build(result);
		} catch (ConnectionException e) {
			throw new RuntimeException(e);
		}
    }
}
